#include "FileRecord.h"


FileRecord::FileRecord()
{
}

FileRecord::~FileRecord(void)
{
	close();
}

int FileRecord::write(const char* pFrame,int iLen,unsigned long timestamp,int type)
{
	FrameHeader fh;
	fh.len = iLen;
	fh.type = type;
	fh.timestamp = timestamp;

	m_fStream.write((const char*)&fh,sizeof(FrameHeader));
	m_fStream.write(pFrame,iLen);
	m_fStream.flush();
	if (m_fStream.good())
		return iLen+sizeof(FrameHeader);

	return -1;
}
int FileRecord::read(FrameHeader& fh,char** pFrame)
{
	m_fStream.read((char*)&fh,sizeof(fh));
	*pFrame = new char[fh.len];
	m_fStream.read(*pFrame,fh.len);
	if (m_fStream.eof())
	{
		return 0;
	}

	if (!m_fStream.good())
	{		
		return -1;
	}

	return fh.len;
}

int FileRecord::open(const string& path)
{
	m_path = path;
	m_fStream.open(m_path,ios::in);
	//m_fStream = new FileStream(m_path);
	return 0;
}
int FileRecord::close()
{
	m_fStream.close();
	return 0;
}
void FileRecord::reset()
{
	close();
	open(m_path);
//	m_fStream->seekg(0, ios::beg);//why invalid when file reach end
//	int pos = m_fStream->tellg();//why tellg return -1
}
